/**      
 * @file		pointerSelectionSort.cpp
 * @description		Demonstrates the use of pointers to pass arguments
 *			by reference. 
 * @course		CSCI 123 Section 00000
 * @assignment 
 * @date		mm/dd/yyyy
 * @author		Brad Rippe (00000000) brippe@fullcoll.edu
 * @version		1.0
 */
#include <iostream>
using namespace std;

/**
 * Sorts an array in ascending order.
 * @param aArray the array to be sorted
 *			the array is passed by reference with pointers
 *			using nonconstant pointer and nonconstant data
 *			both the pointer and the data can be changed
 * @param aSize the size of the array
 */
void selectionSort(int *aArray, int aSize);

/**
 * Swaps the value of two integers
 * @param aElement1 the first integer to be swapped
 * @param aElement2 the second integer to be swapped
 */
void swap(int *aElement1, int *aElement2);

/**
 * Prints an array to standard output
 * @param aArray the array to be output
 *			the array is passed by reference with pointers
 *			using nonconstant pointer and CONSTANT data
 *			only the pointer can be changed and not the data
 * @param aSize the size of the array to be output
 */
void printArray(const int *aArray, int aSize);

/**
 * @return zero if the application executes successfully
 */
int main() {
	int unSortedArray[] = { 23, 0, 56, 1, 45, 78, 2, 34 };

	cout << "Array before sorting\n";
	printArray(unSortedArray, 8);
	selectionSort(unSortedArray, 8);

	cout << "Array after sorting\n";
	printArray(unSortedArray, 8);

	return 0;
}

void selectionSort(int *aArray, int aSize) {
	for(int i = 0; i < aSize-1; i++) {
		int smallest = i;
		for(int j = i+1; j < aSize; j++) {
			if(aArray[j] < aArray[smallest]) {
				smallest = j;
			}
		}
		swap(&aArray[i], &aArray[smallest]);
	}
	
}

void swap(int *aElement1, int *aElement2) {
	int temp = *aElement1;
	*aElement1 = *aElement2;
	*aElement2 = temp;
}

void printArray(const int *aArray, int aSize) {
	for(int i = 0; i < aSize; i++)
		cout << "aArray[" << i << "] = " << *aArray++ << endl;
}
